<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\DistributionList
 *
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionList onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionList withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\DistributionList withoutTrashed()
 * @mixin \Eloquent
 * @property int $id
 * @property int $user_id 用户id
 * @property int $rule_id 当前的分销规则
 * @property int $img_id 头像
 * @property string $name 姓名
 * @property string $phone 手机
 * @property float $total_money 累计获得佣金
 * @property string|null $settling_time 入驻时间
 * @property int|null $type 分销商类型0普通分销商，1特约分销商
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereRuleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereSettlingTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereTotalMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\DistributionList whereUserId($value)
 */
class DistributionList extends Model
{
    use SoftDeletes;
    public static function getMyRule($user_id){
        if (!$user_id)
            return 0;
        $me = DistributionList::whereUserId($user_id)->first();
        if (!$me)
            return 0;
        $rule = DistributionRule::whereId($me->rule_id)->first();
        if (!$rule)
            $rule = DistributionRule::whereIsDefault(1)->orderBy('id','desc')->first();
        return $rule;
    }
    public static function becomeDistribution($user_id){
        if (!$user_id)
            return 0;
        $me = DistributionList::whereUserId($user_id)->first();
        if (!$me){
            $me = new DistributionList();
            $me->user_id    = $user_id;
            $user_info      = UserInfo::getUserInfo($user_id);
            $me->name       = $user_info->nickname ?? '';
            $me->img_id     = $user_info->photo_id ?? 0;
            $me->settling_time     = date('Y-m-d H:i:s');
            $rule = DistributionRule::whereIsDefault(1)->orderBy('id','desc')->first();
            $me->rule_id = $rule ? $rule->id : 0;
            $me->type = 0;
            $me->save();

            $newCommissions = new Commission();
            $newCommissions->user_id = $user_id;
            $newCommissions->available_money = 0;
            $newCommissions->save();
        }
        if ($me->rule_id == 0){
            $rule = DistributionRule::whereIsDefault(1)->orderBy('id','desc')->first();
            $me->rule_id = $rule ? $rule->id : 0;
            $me->type = 0;
            $me->save();
        }

    }
}
